﻿using BizControl.Library.BusinessObjects;
using Csla.Xaml;
using BizControl.Presentation.Infrastructure.Controls;
using Microsoft.Practices.Prism.Regions;
using System.ComponentModel.Composition;
using Microsoft.Practices.Prism.Commands;
using Microsoft.Practices.Prism;
using System;
using BizControl.Presentation.Infrastructure.Constants;
using System.Windows.Input;
using BizControl.Library.BusinessObjects.BusinessRoles;

namespace BizControl.Presentation.BusinessRolesModule.ViewModels
{
  [Export]
  public class BusinessRolesViewModel : ViewModel<BusinessRoleList>, INavigationAware, IRegionMemberLifetime, IPartImportsSatisfiedNotification
  {
    private readonly DelegateCommand<Guid?> _editRoleCommand;
    private readonly DelegateCommand _refreshListCommand;
    private readonly DelegateCommand _addRoleCommand;

    private IRegionManager _regionManager;

    [ImportingConstructor]
    public BusinessRolesViewModel(IRegionManager regionManager)
    {
      _regionManager = regionManager;
      _editRoleCommand = new DelegateCommand<Guid?>(EditRole);
      _refreshListCommand = new DelegateCommand(RefreshList);
      _addRoleCommand = new DelegateCommand(AddRole);
    }

    private void RefreshList()
    {
      DoRefresh(GetList);
    }

    private void AddRole()
    {
      if (Model != null)
        Model.Add(BusinessRoleBO.NewBusinessRole());
    }

    #region Commands

    public ICommand EditRoleCommand
    {
      get { return this._editRoleCommand; }
    }

    public ICommand RefreshListCommand
    {
      get { return this._refreshListCommand; }
    }

    public ICommand AddRoleCommand
    {
      get { return this._addRoleCommand; }
    }

    private void EditRole(Guid? roleId)
    {
      //if (!string.IsNullOrEmpty(activityId.ToString()))
      //{
      //  var uriQuery = new UriQuery();
      //  uriQuery.Add(NavigationParametersNames.IdName, activityId.ToString());
      //  var uri = new Uri(ViewNames.ActivityEditViewName + uriQuery.ToString(), UriKind.Relative);
      //  this._regionManager.RequestNavigate(ShellRegionNames.MainContentRegion, uri);
      //}
    }

    #endregion

    BusinessRoleList GetList()
    {
      return BusinessRoleList.GetBusinessRoleList();
    }

    #region INavigationAware Members

    public bool IsNavigationTarget(NavigationContext navigationContext)
    {
      return true;
    }

    public void OnNavigatedFrom(NavigationContext navigationContext)
    {
    }

    public void OnNavigatedTo(NavigationContext navigationContext)
    {
      RefreshList();
    }

    #endregion

    #region IRegionMemberLifetime Members

    public bool KeepAlive
    {
      get { return true; }
    }

    #endregion

    #region IPartImportsSatisfiedNotification Members

    public void OnImportsSatisfied()
    {
    }

    #endregion
  }
}
